// 上传验证

// 校验内容约束
interface CheckCoditon {
    format?: string[]    // 文件格式
    size?: number         // 文件大小,单位为M
}

type ErrorType = 'size' | 'format' | null

// 参数： 1、文件对象  2、校验内容
export function beforeUploadCheck(file: File, condition: CheckCoditon) {
    const { format, size } = condition;

    const isValidFormat = format ? format.includes(file.type) : true;
    const isValidSize = size ? (file.size / 1024 / 1024 < size) : true;

    let error: ErrorType = null;
    if (!isValidFormat) {
        error = 'format'
    }
    if (!isValidSize) {
        error = 'size'
    }
    return {
        passed: isValidFormat && isValidSize,
        error
    }
}


interface TestProps {
    _id: string;
    name: string;
}

const testData: TestProps[] = [{_id: "1", name: "a"}, {_id: '2', name: 'b'}];
export const arrToObj = <T extends { _id?: string }>(arr: Array<T>) => {
    return arr.reduce((prev, current) => {
        if(current._id){
            prev[current._id] = current;
        }
        return prev;
    }, {} as {[key: string]: T})
}

let res2 = arrToObj(testData)


const testData2: {[key: string]: TestProps} = {
    1: {_id: '1', name: 'a'},
    2: {_id: '2', name: 'b'},
}

export const objToArr = <T>(obj: {[key: string]: T}) => {
    return Object.keys(obj).map(key => obj[key])
}

let res = objToArr(testData2)